//
//  ST_FilterRangeSliderItemView.swift
//  BTVideo
//
//  Created by DouLei on 2019/10/17.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift
import MultiSlider

class ST_FilterRangeSliderItemView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(info_label)
        self.addSubview(content_label)
        self.addSubview(age_slider)
        
        info_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25)
            make.top.equalToSuperview().offset(20)
        }
        
        content_label.snp.makeConstraints { (make) in
            make.left.equalTo(info_label.snp.right).offset(10)
            make.centerY.equalTo(info_label)
            make.right.equalToSuperview().offset(-35)
        }
        
        age_slider.snp.makeConstraints { (make) in
            
            make.height.equalTo(50)
            make.left.equalToSuperview().offset(25)
            make.right.equalToSuperview().offset(-35)
            make.top.equalTo(info_label.snp.bottom)
            
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var info_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .left
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .right
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var age_slider:MultiSlider = {
        
        let control = MultiSlider()
        control.minimumValue = minimum_age
        control.maximumValue = maximum_age
        control.isVertical = false
        control.tintColor = UIColor.init(hex: 0xFEC207)
        control.outerTrackColor = UIColor.init(hex: 0xF5F8FA)
        control.value = [startAge,end_age]
        control.thumbImage = ST_ImageUtils.imageName("IndexItem_icon_选择")
        control.showsThumbImageShadow = false
        control.addTarget(self, action: #selector(sliderChanged(slider:)), for: UIControl.Event.valueChanged)
        return control
    }()
    
    fileprivate var startAge = 18.cgFloat
    
    fileprivate var end_age = 50.cgFloat
    
    fileprivate var minimum_age = 18.cgFloat
    
    fileprivate var maximum_age = 50.cgFloat
    
//    MARK:- Closure
    var didChange:((Int,Int)->())?
    
//    MARK: - Event

    
//    MARK: - Bind Model
    var model:ST_FilterOptionRangeModel?
    
   func bind(model:ST_FilterOptionRangeModel){
    
        self.model = model
        self.info_label.text = model.title
    
        self.content_label.text = model.minValue.string+"-"+model.maxValue.string
       
       startAge = model.minValue.cgFloat
        end_age = model.maxValue.cgFloat
        
        age_slider.value = [startAge,end_age]
    
   }
    
    
    func bind(option:PinterestFilterOptionModel){
     
         self.info_label.text = option.keyString
     
         self.content_label.text = option.minValue.string+"-"+option.maxValue.string
        
        startAge = option.minValue.cgFloat
         end_age = option.maxValue.cgFloat
         
         age_slider.value = [startAge,end_age]
     
    }
    
    
    @objc func sliderChanged(slider:MultiSlider)  {
        
        if let s0 = slider.value.first{
            startAge = s0
        }
        if let s1 = slider.value[safe:1]{
            end_age = s1
        }
        content_label.text = "\(startAge.int)-\(end_age.int)"
        
        self.didChange?(startAge.int,end_age.int)
    }
}


/*
extension ST_FilterRangeSliderItemView:RangeSeekSliderDelegate{
    
    func rangeSeekSlider(_ slider: RangeSeekSlider, didChange minValue: CGFloat, maxValue: CGFloat) {
        
        content_label.text = minValue.int.string+"-"+maxValue.int.string
        
        self.didChange?(minValue.int,maxValue.int)
    }
    
}
*/



//MARK: -
class ST_FilterSliderItemView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(info_label)
        self.addSubview(content_label)
        self.addSubview(sliderView)
        
        info_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25)
            make.top.equalToSuperview().offset(20)
        }
        
        content_label.snp.makeConstraints { (make) in
            make.left.equalTo(info_label.snp.right).offset(10)
            make.centerY.equalTo(info_label)
            make.right.equalToSuperview().offset(-35)
        }
        
        sliderView.snp.makeConstraints { (make) in
            
            make.height.equalTo(50)
            make.left.equalToSuperview().offset(28)
            make.right.equalToSuperview().offset(-28)
            make.top.equalTo(info_label.snp.bottom)
            
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var info_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .left
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .right
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    

    lazy var sliderView:UISlider = {
        
        let slider = UISlider()
        slider.addTarget(self, action: #selector(slider_value_changed(sender:)), for: UIControl.Event.valueChanged)
        slider.minimumValue = 1
        slider.maximumValue = 200
        slider.setThumbImage(ST_ImageUtils.imageName("IndexItem_icon_选择"), for: UIControl.State.normal)
        slider.tintColor = UIColor.init(hexString: "#FEC207")
//        slider.backgroundColor = UIColor.init(hexString: "#DDDDDD")
        return slider
    }()
   
    var didChange:((Int)->())?
    
//    MARK: - Event
    @objc func slider_value_changed(sender:UISlider){
        
        let str = sender.value.int.string + " km"
         
        content_label.text = str
        
        didChange?(sender.value.int)
    }
    
//    MARK: - Bind Model
    var model:ST_FilterOptionRangeModel?
    
   func bind(model:ST_FilterOptionRangeModel){
    
    self.model = model
    self.info_label.text = model.title
    
    self.content_label.text = model.slideValue.string + " km"
    
    sliderView.value = model.slideValue.float

   }
    
    
    func bind(option:PinterestFilterOptionModel){
     
        self.info_label.text = option.keyString
        
        self.content_label.text = option.value + " km"
     
        sliderView.value = option.value.float() ?? 0

    }
    
    
}

